package cn.net.zj.scan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.maning.mndialoglibrary.MProgressDialog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.net.zj.scan.R;
import cn.net.zj.scan.adapter.TaskListDetailAdapter;
import cn.net.zj.scan.api.HttpMethods;
import cn.net.zj.scan.base.CommonActivity;
import cn.net.zj.scan.base.Result;
import cn.net.zj.scan.bean.TaskDetailBean;
import cn.net.zj.scan.utils.MProgressDialogHelper;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class TaskDetailListActivity extends CommonActivity {

    //<editor-fold desc=”成员变量”>

    @BindView(R.id.tvBillno)
    TextView tvBillno;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private TaskListDetailAdapter taskListDetailAdapter;
    //</editor-fold>


    //<editor-fold desc=”生命周期”>

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_detail_list);
        ButterKnife.bind(this);
        configUI();
    }

    //</editor-fold>

    //<editor-fold desc=”私有方法”>
    private void configUI() {
        final String fBillNo = getIntent().getStringExtra("FBillNo");
        tvBillno.setText("当前任务编号："+fBillNo);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        taskListDetailAdapter = new TaskListDetailAdapter(R.layout.item_task_detail, null);
        taskListDetailAdapter.bindToRecyclerView(recyclerView);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        taskListDetailAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TaskDetailBean item = (TaskDetailBean) adapter.getItem(position);
                Intent intent = new Intent(TaskDetailListActivity.this, InHouseActivity.class);
                intent.putExtra("FBillNo", fBillNo);
                ActivityUtils.startActivity(intent);
            }
        });

        requestTaskDetail();
    }
    //</editor-fold>

    //<editor-fold desc=”网络请求”>
    private void requestTaskDetail() {
        String fBillNo = getIntent().getStringExtra("FBillNo");
        MProgressDialogHelper.showLoading(this);
        HttpMethods.getInstance().getFyApi().getTaskDetail(fBillNo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleObserver<Result<ArrayList<TaskDetailBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onSuccess(Result<ArrayList<TaskDetailBean>> value) {
                        MProgressDialog.dismissProgress();
                        if (value.getData() != null && value.getData().size() > 0) {
                            taskListDetailAdapter.setNewData(value.getData());
                        } else {
                            ToastUtils.showLong(value.getMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        MProgressDialog.dismissProgress();
                        ToastUtils.showLong(e.getMessage()+"");
                    }
                });

    }
    //</editor-fold>
}